package com.feizhi.inventory.fragment.exportdetails.exportalready;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.aserbao.aserbaosandroid.functions.database.greenDao.db.DaoSession;
import com.aserbao.aserbaosandroid.functions.database.greenDao.db.TaskDetailsDao;
import com.feizhi.inventory.MainActivity;
import com.feizhi.inventory.R;
import com.feizhi.inventory.database.TaskDetails;
import com.feizhi.inventory.database.dbApp;
import com.feizhi.inventory.databinding.FragmentExportAlreadyBinding;
import com.feizhi.inventory.util.ThreadPool;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

/**
 *  数据导出的已盘点
 *
 **/
public class ExportAlreadyFragment extends Fragment {
    private List<TaskDetails> details;
    private FragmentExportAlreadyBinding mBinding;
    private MainActivity mActivity;

    private TaskDetailsDao taskDetailsDao;
    private DaoSession updaoSession;
    private List<TaskDetails> taskDetailsList = new ArrayList<>();
    List<TaskDetails> lists;
    private ExportAlreadyAdapter adapter;
    private String task_id;

    public ExportAlreadyFragment() {
        // Required empty public constructor
    }



    /**
     * 视图创建的时候调用，Android中Activity的生命周期
     * */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity) getActivity();
        updaoSession = ((dbApp) mActivity.getApplication()).getDaoSession();

    }

    /**
     *  Fragment的生命周期
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //Data绑定
        mBinding = FragmentExportAlreadyBinding.inflate(inflater);
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        setOnClickListener();
    }

    /**
     *  控件的点击事件
     * */
    private void setOnClickListener() {

        //对应每个Item的跳转
        adapter.setItemOnClickListener(new ExportAlreadyAdapter.OnClickListener() {
            @Override
            public void OnItemClickListener(int position) {

                //Bundle打包数据去下一个页面
                Bundle bundle = new Bundle();
                bundle.putString("id", taskDetailsList.get(position).getId());
                bundle.putString("name", taskDetailsList.get(position).getName());
                bundle.putString("spec", taskDetailsList.get(position).getSpec());
                bundle.putString("eps_id", taskDetailsList.get(position).getEpcID());
                bundle.putString("save_place", taskDetailsList.get(position).getSavePlace());
                bundle.putString("custodian", taskDetailsList.get(position).getCustodian());
                bundle.putString("department", taskDetailsList.get(position).getDepartment());
                bundle.putString("state", taskDetailsList.get(position).getState());
                bundle.putString("notes_1", taskDetailsList.get(position).getNotes_1());
                bundle.putString("notes_2", taskDetailsList.get(position).getNotes_2());
                bundle.putString("notes_3", taskDetailsList.get(position).getNotes_3());
                bundle.putString("type", taskDetailsList.get(position).getType());
                bundle.putString("inputDate", taskDetailsList.get(position).getInputDate());
                //fragment跳转
                Navigation.findNavController(mActivity, R.id.fragment_my).navigate(R.id.itemDetailsFragment, bundle);

            }
        });


    }

    private void initView() {

        //创建GreenDao对象，初始化操作
        DaoSession daoSession = ((dbApp) mActivity.getApplication()).getDaoSession();
        taskDetailsDao = daoSession.getTaskDetailsDao();

        //初始化RecyclerView，设置RecyclerView的Adapter
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new ExportAlreadyAdapter(taskDetailsList, mActivity);
        mBinding.recyclerView.setAdapter(adapter);

        //Event接收数据，注册接收者可接受，采用的是观察者模式
        LiveEventBus.get().with("tid", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {

                task_id = s;

                onRefresh();
            }
        });



    }

    //刷新数据
    public void onRefresh() {
        //初始化一个进度条对话框，用来提示加载数据
        ProgressDialog progressDialog = new ProgressDialog(mActivity);
        progressDialog.setTitle("加载数据");
        progressDialog.setMessage("正在加载ing...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();
        //创建一个子线程，此方法可优化
        // TODO: 2023/7/17 后期可优化，可用线程池
        ThreadPool.runThread(() -> {
                //查相对应表中的数据，赋值给lists集合
                lists = taskDetailsDao.queryBuilder().where(TaskDetailsDao.Properties.TaskID.eq(task_id), TaskDetailsDao.Properties.State.eq("已盘点")).list();
                //重新加载要先清楚，不然会显示重复的数据
                if (!taskDetailsList.isEmpty()) {
                    taskDetailsList.clear();
                }
                //把数据赋值给taskDetailsList集合
                taskDetailsList.addAll(lists);
                //回到主线程更新UI，子线程更新UI会报错
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //此方法中有更新UI的操作
                        adapter.setData(taskDetailsList);
                        //关闭进度条圈圈
                        progressDialog.dismiss();
                    }
                });
        });


    }
}